package service.impl;

import global.Constants;
import global.security.SessionUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import service.PictureService;
import util.FileUtils;
import util.StringUtils;
import dao.PictureDAO;
import domain.Picture;
import domain.PictureWithBLOBs;

/**
 * 图片Service实现类.
 * <p>
 * 创建日期：2010-07-01<br>
 * 创建人：Xiyt<br>
 * 修改日期：<br>
 * 修改人：<br>
 * 修改内容：<br>
 * 
 * @author Xiyt
 * @version 1.0
 */
public class PictureServiceImpl implements PictureService {
	/**
	 * 图片DAO
	 * */
	private PictureDAO pictureDao = null;

	/**
	 * 设定图片DAO
	 * */
	public void setPictureDao(PictureDAO pictureDao) {
		this.pictureDao = pictureDao;
	}

	/**
	 * 根据图片ID查询图片信息
	 * @param pictureId 图片ID
	 * @return Picture 图片
	 */
	public Picture selectByPrimaryKey(int pictureId) {
		Picture picture = pictureDao.selectByPrimaryKey(pictureId);
		return picture;
	}


	/**
	 * 根据条件查询图片数量
	 * @Description: 根据条件查询图片数量
	 * @param paramPicture 图片
	 * @return int 图片数量
	 */
	public int countByCriteria(Picture paramPicture) {
		// 根据条件查询图片数量
		paramPicture = getConditions(paramPicture);
		int count = pictureDao.countByCriteria(paramPicture);
		return count;
	}

	/**
	 * 根据条件查询图片列表WithoutBLOBs
	 * @param paramPicture 图片
	 * @return list 图片列表
	 */
	public List<Picture> selectByCriteria(Picture paramPicture) {
		// 取得pictureList
		paramPicture = getConditions(paramPicture);
		List<Picture> pictureList = pictureDao.selectByCriteria(paramPicture);
		return pictureList;
	}

	/**
	 * 根据条件查询图片列表带分页WithoutBLOBs
	 * @param paramPicture 图片
	 * @return list 图片列表
	 */
	public List<Picture> selectByCriteriaForPaging(
			Picture paramPicture) {
		// 取得pictureList
		paramPicture = getConditions(paramPicture);
		List<Picture> pictureList = pictureDao.selectByCriteriaForPaging(paramPicture,
						paramPicture.getStart(), paramPicture.getLimit());
		if (pictureList != null) {
			List<Picture> resultList = new ArrayList<Picture>();
			for (Picture picture : pictureList) {
				resultList.add(picture);
			}
			return resultList;
		}
		return null;
	}

	/**
	 * 添加图片
	 * @param picture 图片
	 */
	public void insert(PictureWithBLOBs picture) {
		// 用户ID
		String userId = SessionUtils.getCurrentUserId();
		if(StringUtils.isEmpty(userId)){// 文件上传时，CLIENT_SESSION为空
			userId = picture.getCreateUser();
		}
		// 删除区分：未删除
		picture.setDelflag(Constants.DEL_FLAG_1);
		// 系统时间
		Timestamp sysdate = new Timestamp(System.currentTimeMillis());
		picture.setCreateUser(userId);
		picture.setCreateTime(sysdate);
		picture.setUpdateUser(userId);
		picture.setUpdateTime(sysdate);
		pictureDao.insert(picture);
	}

	/**
	 * 修改图片
	 * @param picture 图片
	 */
	public int update(PictureWithBLOBs picture) {
		// 系统时间
		Timestamp sysdate = new Timestamp(System.currentTimeMillis());
		picture.setUpdateUser(SessionUtils.getCurrentUserId());
		picture.setUpdateTime(sysdate);
		return pictureDao.updateByPrimaryKey(picture);
	}

	/**
	 * 修改图片名称
	 * @param pictureIds 图片IDS
	 * @param pictureNames 图片名称
	 */
	public void update(String pictureIds, String pictureNames) throws Exception{
		if(StringUtils.isNotEmpty(pictureIds) && StringUtils.isNotEmpty(pictureNames)){
			String[] pictureIdArr = pictureIds.split(",");
			String[] pictureNameArr = pictureNames.split(",");
			for(int i=0;i<pictureIdArr.length;i++){
				PictureWithBLOBs temp = new PictureWithBLOBs();
				temp.setPictureId(Integer.parseInt(pictureIdArr[i]));
				temp.setPictureName(pictureNameArr[i]);
				// 更新图片
				pictureDao.updateByPrimaryKey(temp);
			}
		}
	}

	/**
	 * 移动图片
	 * @param pictureIds 图片IDS
	 * @param albumId 相册ID
	 */
	public void move(String pictureIds, String albumId){
		if(StringUtils.isNotEmpty(pictureIds) && StringUtils.isNotEmpty(albumId)){
			String[] pictureIdArr = pictureIds.split(",");
			PictureWithBLOBs temp = new PictureWithBLOBs();
			temp.setAlbumId(Integer.parseInt(albumId));
			for(int i=0;i<pictureIdArr.length;i++){
				temp.setPictureId(Integer.parseInt(pictureIdArr[i]));
				// 更新图片
				pictureDao.updateByPrimaryKey(temp);
			}
		}
	}

	/**
	 * 删除图片
	 * @param pictureIds 图片IDS
	 * @param uuids 图片UUIDS
	 * @param uploadFilePath 文件路径
	 * @throws Exception 
	 */
	public void delete(String pictureIds, String uuids, String uploadFilePath) throws Exception{
		String[] pictureIdArr = pictureIds.split(",");
		String[] uuidArr = uuids.split(",");
		for(int i=0;i<pictureIdArr.length;i++){
			// 参数设定
			// 删除数据库图片记录
			pictureDao.deleteByPrimaryKey(Integer.parseInt(pictureIdArr[i]));
			
			// 原图路径
			String lpath = uploadFilePath + "/" + uuidArr[i];
			// 缩略图路径
			String spath = uploadFilePath + "/" + Constants.THUMBNAIL + "/" + FileUtils.getFileName(uuidArr[i]) + "_s." + FileUtils.getFileTypeByName(uuidArr[i]);
			// 删除磁盘原图
			FileUtils.delete(lpath);
			// 删除磁盘缩略图
			FileUtils.delete(spath);
		}
	}
	
	
	/**
	 * 删除图片
	 * @param paramPicture 图片
	 */
	public int deleteByPrimaryKey(int id){
		return pictureDao.deleteByPrimaryKey(id);
	}
	
	
	/**
	 * 设置公共参数
	 * @param paramPicture 参数对象
	 */
	private Picture getConditions(Picture paramPicture){
		paramPicture.setCreateUser(SessionUtils.getCurrentUserId());// 当前用户
		return paramPicture;
	}
}
